package com.hmall.common.config;


import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;

import javax.sql.DataSource;
import java.sql.Connection;

@RequiredArgsConstructor
@Configuration
@ConditionalOnClass(DispatcherServlet.class)
public class HikariInitConfig implements CommandLineRunner {
    private final DataSource dataSource;
    @Override
    public void run(String... args) throws Exception {
//        Connection connection = null;
//        try {
//            connection = dataSource.getConnection();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        try-with-resource将他释放
        try  (Connection connection = dataSource.getConnection()) {
            System.out.println(connection);
        }
    }
}
